from flask import Flask
from flask import render_template
from flask import request, send_from_directory
import json
import os
from docx import Document
import pandas as pd
from concurrent.futures import ThreadPoolExecutor

from doctoexcel import parserDoc
import time
from doctoexcel.parserDoc import parseException


app = Flask(__name__)

root_dir = os.path.dirname(os.path.realpath(__file__))
resource_dir = os.path.join(root_dir, 'resource')
# 已经匹配过的异常id 用于过滤
exception_data_id_already = set()
# 匹配度
match_degree = 80


@app.route("/")
def upload_demo():
    """上传页面"""
    return render_template("upload.html", page_title="批量上传")


@app.route("/file/<action>", methods=['post', 'get'])
def file_func(action):
    excelFileName = '简历批量处理' + time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime()) + '.xlsx'
    xls = pd.ExcelWriter(excelFileName)  # 用来写入 excel
    head = ['姓名', '性别', '电话', '求职意向', '毕业院校', '专业', '毕业时间', '出生年月', '学历', '籍贯', '邮箱', '工作经历']
    mes = {"message": "success", "filename": excelFileName}
    try:
        if action == "save":
            mat = [head]
            for key_name, file_storage in request.files.items():
                if file_storage is not None:
                    if key_name.endswith('pdf'):
                        tmpstr = parserDoc.parserPdf(file_storage)
                    else:
                        doc = Document(file_storage)  # word 文件
                        # 解析单个文件 并返回list
                        tmpstr = parserDoc.parseDoc(doc)
                    # 字符串解析
                    print(tmpstr)
                    row = parserDoc.parseArrayInfo(tmpstr)
                    mat.append(row)
                    file_storage.close()
            df = pd.DataFrame(mat)
            df.to_excel(xls, sheet_name=f'简历登记', index=False, header=None)
        xls.save()
        xls.close()
    except Exception as e:
        print('发送异常:', e)
        mes = {"message": "出错啦", "filename": excelFileName}
    return json.dumps(mes)


@app.route("/downLoad/<filename>")
def downLoad(filename):
    return send_from_directory(root_dir,
                               filename=filename, as_attachment=True)


@app.route("/excel")
def excelParse():
    """标准库解析"""
    return render_template("/excel.html", page_title="标准库解析")


@app.route("/excel/parse/<action>", methods=['post', 'get'])
def parseExcel(action):
    excFileName = '匹配结果数据' + time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime()) + '.xlsx'
    xls = pd.ExcelWriter(excFileName)
    mes = {"message": "success", "filename": excFileName}
    try:
        if action == "save":
            for key_name, file_storage in request.files.items():
                if file_storage is not None:
                    # 读取 标准库sheet
                    start_time = time.time()

                    standard_data_raw = pd.read_excel(request.files.get(key_name), '标准库')
                    exception_data_raw = pd.read_excel(request.files.get(key_name), '异常库匹配')
                    with ThreadPoolExecutor(max_workers=1000) as t:  # 创建一个最大容纳数量为5的线程池
                        for i in standard_data_raw.index.values:
                            standard_data = standard_data_raw.iloc[i, 0:].to_dict()
                            global exception_data_id_already
                            print('标准异常id=', standard_data['标准异常id'])
                            print('已匹配数量', len(exception_data_id_already))
                            t.submit(processExceptionData,
                                     exception_data_raw, exception_data_raw.index.stop, standard_data)
                        # for j in exception_data_raw.index.values:

                    # while True:
                    #     # 等待所有线程完成
                    #     lock.acquire()
                    #     if unfinished_thread != 0:
                    #         lock.release()
                    #         time.sleep(1)
                    #         print
                    #         '多线程未结束，休眠1s，剩余线程数量：%s' % unfinished_thread
                    #     else:
                    #         lock.release()
                    #         break
                    # print
                    # '运行完毕,耗时%s秒' % (time.time() - start_time)
                    time.sleep(5)
                    print('运行完毕 耗时', time.time() - start_time)
                    exception_data_raw.to_excel(xls, index=False)
                    exception_data_id_already.clear()
            xls.save()
            xls.close()
    except Exception as e:
        print('发送异常:', e)
        mes = {"message": "出错啦", "filename": excFileName}
    return json.dumps(mes)


def processExceptionData(exception_data_raw, idx_scope, standard_data):
    print('开始线程处理 ===')
    global exception_data_id_already
    for j in range(0, idx_scope):
        # 过滤调 na 值
        if not pd.isna(exception_data_raw.iloc[j, 0]):
            exception_data = exception_data_raw.iloc[j, 0:].to_dict()
            if exception_data['异常别名id'] in exception_data_id_already:
                print(exception_data['异常别名'], '已经匹配')
            else:
                degree = parseException(exception_data['异常别名'], standard_data['标准异常名称'])
                if degree >= match_degree:
                    exception_data_id_already.add(exception_data['异常别名id'])
                    exception_data_raw.iloc[j, 2] = standard_data['标准异常id']
                    exception_data_raw.iloc[j, 3] = standard_data['标准异常名称']
                    exception_data_raw.iloc[j, 4] = degree
    print('线程处理结束')


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=9999)
